package com.jic.manager.controller.manage.conf;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.entity.WxPublicNum;
import com.jic.conf.request.WxPublicNumRequest;
import com.jic.conf.response.WxPublicNumResponse;
import com.jic.manager.service.WxPublicNumService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZW on 2020/1/9 11:04
 * 商品计量单位管理
 */
@Slf4j
@RestController
@RequestMapping("/wxPublicNum")
@Api(value = "微信公众号配置", description = "微信公众号配置", tags = "微信公众号配置")
public class WxPublicNumController {

    @Autowired
    private WxPublicNumService wxPublicNumService;


    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取微信公众号配置信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<WxPublicNumResponse>> queryPage(@RequestBody WxPublicNumRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        WxPublicNum wxPublicNum = BeanUtil.copy(form, WxPublicNum.class);
        PageResult<WxPublicNum> result = wxPublicNumService.queryPage(wxPublicNum, page);
        List<WxPublicNumResponse> WxPublicNumResponseList = result.getRows().stream().map(temp -> {
            WxPublicNumResponse WxPublicNumResponse = BeanUtil.copy(temp, WxPublicNumResponse.class);
            return WxPublicNumResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<WxPublicNumResponse> pageResult = new PageResult<>();
        pageResult.setRows(WxPublicNumResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }


    /*
     *  微信公众号配置新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editWxPublicNum")
    public RestResult insertWxPublicNum(@RequestBody WxPublicNumRequest wxPublicNumRequest) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (wxPublicNumRequest.getId() !=null) {
                wxPublicNumService.update(wxPublicNumRequest);
                restResult = RestResult.success(restResult);
            } else {//新增
                wxPublicNumService.insert(wxPublicNumRequest);
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;

    }

    /*
     *  微信公众号配置删除
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteWxPublicNum")
    public RestResult deleteWxPublicNum( @RequestBody WxPublicNum id) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = wxPublicNumService.delete(id);
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;

    }
}
